package com.nep.NEPM.controller;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

public class AdminViewController {
    // 公共组件
    @FXML private Label totalCountLabel;
    @FXML private Label goodCountLabel;

    // AQI指数表格
    @FXML private TableView<AQIData> aqiTableView;
    @FXML private TableColumn<AQIData, String> colLevel;
    @FXML private TableColumn<AQIData, String> colDescription;
    @FXML private TableColumn<AQIData, Integer> colCount;

    // 省分组统计表格
    @FXML private TableView<ProvinceData> provinceTable;
    @FXML private TableColumn<ProvinceData, String> colProvinceId;
    @FXML private TableColumn<ProvinceData, String> colProvinceShort;
    @FXML private TableColumn<ProvinceData, String> colProvinceName;
    @FXML private TableColumn<ProvinceData, Integer> colSO2;
    @FXML private TableColumn<ProvinceData, Integer> colCO2;
    @FXML private TableColumn<ProvinceData, Integer> colPM25;
    @FXML private TableColumn<ProvinceData, Integer> colAQI;

    // AQI趋势表格
    @FXML private TableView<AQITrendData> trendTable;
    @FXML private TableColumn<AQITrendData, String> colSeq;
    @FXML private TableColumn<AQITrendData, String> colMonth;
    @FXML private TableColumn<AQITrendData, Integer> colExceedCount;

    // 其他统计面板
    @FXML private GridPane otherStatsGrid;

    @FXML
    private void initialize() {
        // 初始化AQI指数表格
        setupAQITable();

        // 初始化省分组统计表格
        setupProvinceTable();

        // 初始化AQI趋势表格
        setupTrendTable();

        // 初始化其他统计数据
        setupOtherStats();
    }

    // AQI指数表格配置
    private void setupAQITable() {
        colLevel.setCellValueFactory(new PropertyValueFactory<>("level"));
        colDescription.setCellValueFactory(new PropertyValueFactory<>("description"));
        colCount.setCellValueFactory(new PropertyValueFactory<>("count"));

        // 加载测试数据
        aqiTableView.setItems(FXCollections.observableArrayList(
                new AQIData("优", "空气质量良好", 120),
                new AQIData("良", "轻微污染", 85),
                new AQIData("轻度污染", "敏感人群不适", 30)
        ));
    }

    // 省分组表格配置
    private void setupProvinceTable() {
        colProvinceId.setCellValueFactory(new PropertyValueFactory<>("provinceId"));
        colProvinceShort.setCellValueFactory(new PropertyValueFactory<>("provinceShort"));
        colProvinceName.setCellValueFactory(new PropertyValueFactory<>("provinceName"));
        colSO2.setCellValueFactory(new PropertyValueFactory<>("so2Exceed"));
        colCO2.setCellValueFactory(new PropertyValueFactory<>("co2Exceed"));
        colPM25.setCellValueFactory(new PropertyValueFactory<>("pm25Exceed"));
        colAQI.setCellValueFactory(new PropertyValueFactory<>("aqiExceed"));

        // 加载测试数据
        provinceTable.setItems(FXCollections.observableArrayList(
                new ProvinceData("001", "京", "北京市", 12, 8, 25, 40),
                new ProvinceData("002", "沪", "上海市", 8, 5, 18, 32)
        ));
    }

    // AQI趋势表格配置
    private void setupTrendTable() {
        colSeq.setCellValueFactory(new PropertyValueFactory<>("sequence"));
        colMonth.setCellValueFactory(new PropertyValueFactory<>("month"));
        colExceedCount.setCellValueFactory(new PropertyValueFactory<>("exceedCount"));

        // 加载测试数据
        trendTable.setItems(FXCollections.observableArrayList(
                new AQITrendData("1", "1月", 15),
                new AQITrendData("2", "2月", 12),
                new AQITrendData("3", "3月", 18)
        ));
    }

    // 其他统计配置
    private void setupOtherStats() {
        totalCountLabel.setText("256");
        goodCountLabel.setText("185");
    }

    // 按钮事件处理
    @FXML private void handleLogout() { System.exit(0); }
    @FXML private void handleSupervisor() { showAlert("功能提示", "公众监督数据列表功能待实现"); }
    @FXML private void handleConfirmAQI() { showAlert("功能提示", "确认AQI数据列表功能待实现"); }
    @FXML private void handleProvinceStats() { showProvinceStats(); }
    @FXML private void handleAqiDistribution() { showAQIDistribution(); }
    @FXML private void handleAqiTrend() { showAQITrend(); }
    @FXML private void handleOtherStats() { showOtherStats(); }

    // 视图切换方法
    private void showProvinceStats() {
        provinceTable.setVisible(true);
        aqiTableView.setVisible(false);
        trendTable.setVisible(false);
        otherStatsGrid.setVisible(false);
    }

    private void showAQIDistribution() {
        aqiTableView.setVisible(true);
        provinceTable.setVisible(false);
        trendTable.setVisible(false);
        otherStatsGrid.setVisible(false);
    }

    private void showAQITrend() {
        trendTable.setVisible(true);
        aqiTableView.setVisible(false);
        provinceTable.setVisible(false);
        otherStatsGrid.setVisible(false);
    }

    private void showOtherStats() {
        otherStatsGrid.setVisible(true);
        aqiTableView.setVisible(false);
        provinceTable.setVisible(false);
        trendTable.setVisible(false);
    }

    private void showAlert(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    // 数据模型类
    public static class AQIData {
        private final String level;
        private final String description;
        private final int count;

        public AQIData(String level, String description, int count) {
            this.level = level;
            this.description = description;
            this.count = count;
        }

        // 添加新的构造函数
        public AQIData(String level, int count) {
            this.level = level;
            this.description = ""; // 可以根据实际情况设置默认值
            this.count = count;
        }

        public String getLevel() { return level; }
        public String getDescription() { return description; }
        public int getCount() { return count; }
    }

    public static class ProvinceData {
        private final String provinceId;
        private final String provinceShort;
        private final String provinceName;
        private final int so2Exceed;
        private final int co2Exceed;
        private final int pm25Exceed;
        private final int aqiExceed;

        public ProvinceData(String id, String shortName, String name,
                            int so2, int co2, int pm25, int aqi) {
            this.provinceId = id;
            this.provinceShort = shortName;
            this.provinceName = name;
            this.so2Exceed = so2;
            this.co2Exceed = co2;
            this.pm25Exceed = pm25;
            this.aqiExceed = aqi;
        }

        public String getProvinceId() { return provinceId; }
        public String getProvinceShort() { return provinceShort; }
        public String getProvinceName() { return provinceName; }
        public int getSo2Exceed() { return so2Exceed; }
        public int getCo2Exceed() { return co2Exceed; }
        public int getPm25Exceed() { return pm25Exceed; }
        public int getAqiExceed() { return aqiExceed; }
    }

    public static class AQITrendData {
        private final String sequence;
        private final String month;
        private final int exceedCount;

        public AQITrendData(String seq, String month, int count) {
            this.sequence = seq;
            this.month = month;
            this.exceedCount = count;
        }

        public String getSequence() { return sequence; }
        public String getMonth() { return month; }
        public int getExceedCount() { return exceedCount; }
    }
}